package com.huluobo.a2107mvpimdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2107mvpimdemo.R;
import com.huluobo.a2107mvpimdemo.view.ReceiveMessageView;
import com.huluobo.a2107mvpimdemo.view.SendMessageView;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.util.DateUtils;

import java.util.List;

/**
 * Created by LC on 2023/10/6.
 */
public class MessageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int ITEM_TYPE_SEND_MESSAGE = 0;
    public static final int ITEM_TYPE_RECEIVE_MESSAGE = 1;

    //根据数据源中消息发送的方向,来给item来赋值view的Type
    //多布局的关键
    @Override
    public int getItemViewType(int position) {
        if (messages.get(position).direct() == EMMessage.Direct.SEND) {
            return ITEM_TYPE_SEND_MESSAGE;
        } else {
            return ITEM_TYPE_RECEIVE_MESSAGE;
        }
    }

    private List<EMMessage> messages;

    public MessageListAdapter(List<EMMessage> messages) {
        this.messages = messages;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //根据上面传入的type来确定返回那个item的布局
        if (viewType == ITEM_TYPE_SEND_MESSAGE) {
            return new SendMessageViewHolder(
                    new SendMessageView(parent.getContext(), null)
            );
        } else {
            return new ReceiveMessageViewHolder(
                    new ReceiveMessageView(parent.getContext(), null)
            );
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        boolean showTimestamp = isShowTimestamp(position);
        if (getItemViewType(position) == ITEM_TYPE_SEND_MESSAGE) {
            SendMessageView sendMessageView = (SendMessageView) holder.itemView;
            sendMessageView.bindView(messages.get(position), showTimestamp);
        } else {
            ReceiveMessageView receiveMessageView = (ReceiveMessageView) holder.itemView;
            receiveMessageView.bindView(messages.get(position), showTimestamp);
        }
    }

    private boolean isShowTimestamp(int position) {
        //如果是第一条消息,或者与之前一条间隔时间过长(30秒)
        boolean showTimestamp = true;
        if (position > 0) {
            showTimestamp = !DateUtils.isCloseEnough(messages.get(position).getMsgTime(),
                    messages.get(position - 1).getMsgTime());
        }
        return showTimestamp;
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    //接收消息的viewHolder
    static class SendMessageViewHolder extends RecyclerView.ViewHolder {
        public SendMessageViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    //发送消息的viewHolder
    static class ReceiveMessageViewHolder extends RecyclerView.ViewHolder {

        public ReceiveMessageViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
